package com.xiaochong.tir.manager.controller;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.tir.common.data.po.Account;
import com.xiaochong.tir.common.util.Constants;
import com.xiaochong.tir.manager.enums.ErrorEnum;
import com.xiaochong.tir.manager.filter.SessionManager;
import com.xiaochong.tir.manager.service.AccountService;
import com.xiaochong.tir.manager.util.EncodeUtils;
import com.xiaochong.tir.manager.util.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author xufanglin
 * @date 2017-12-07 11:55 Created by xufanglin on 2017/12/7 0007.
 */
@Controller
@Api("登陆")
public class LoginController {

	@Autowired
	private AccountService accountService;
	@Autowired
	private OSSTool ossTool;

	/**
	 * 登陆页面
	 * @return
	 */
	@RequestMapping("/login")
	public String index() {
		return Constants.LOGIN;
	}

	/**
	 * 登陆页面
	 * @return
	 */
	@RequestMapping("/loginSys")
	public String loginSys() {
		return Constants.LOGIN;
	}

	/**
	 * 账号登陆
	 * 
	 * @param map
	 * @return
	 */
	@ApiOperation(value = "账号登陆",notes = "账号登陆")
	@RequestMapping(value = "/loginSys", method = RequestMethod.POST)
	public String login(@RequestParam("userName") String userName, @RequestParam("password") String password,
			@RequestParam("vrifyCode") String vrifyCode, HttpSession session, Map<String, Object> map) {
		try {
			// 验证码校验
			Object svrifyCode = session.getAttribute("vrifyCode");
			if (svrifyCode != null && !svrifyCode.toString().equals(vrifyCode)) {
				map.put(Constants.MSG, ErrorEnum.USERNAME_VRIFYCODE_ERROR.getMsg());
				return Constants.LOGIN;
			}
			Account account = accountService.loginBack(userName, password);
			if (account == null) {
				// 登录失败
				map.put(Constants.MSG, ErrorEnum.USERNAME_PASSWORD_ERROR.getMsg());
				return Constants.LOGIN;
			}
			if (account.getStatus().equals(0) ) {
				// 登录无效
				map.put(Constants.MSG, ErrorEnum.USERNAME_PASSWORD_INVALID.getMsg());
				return Constants.LOGIN;
			}
			if(StringUtils.isNotBlank(account.getHead())&&!account.getHead().contains("http")){
				account.setHead(ossTool.getOSSUrl(account.getHead()));
			}
			SessionManager.setLoginSession(session, account);
		} catch (Exception ex) {
			LogTrace.error(getClass().getSimpleName() + "/loginSys", ex.getMessage(), ex);
		}
		return Constants.INDEX;
	}

	/**
	 * 修改密码页面
	 * @return
	 */
	@RequestMapping("/modifyPage")
	public String modifyPage() {
		return Constants.MODIFY_PAGE;
	}

	/**
	 * 修改密码
	 * @param password
	 * @param newpwd
	 * @param session
	 * @return
	 */
	@ApiOperation(value = "修改密码页面",notes = "登陆成功后右上角修改个人密码")
	@RequestMapping("/modifyPWD")
	@ResponseBody
	public Object modifyPWD(@RequestParam("password") String password, @RequestParam("newpwd") String newpwd,
			HttpSession session) {
		try {
			Account account = accountService.getAccountById(SessionManager.getLoginAccount(session).getId());

			password = EncodeUtils.md5Encode(password);
			if (!password.equals(account.getPassword())) {
				return new ReturnResult<String>(0, "原密码错误！", "0", 0);
			} else {
				account.setPassword(EncodeUtils.md5Encode(newpwd));
				accountService.editAccount(account);
				return new ReturnResult<String>(0, "修改成功！", "1", 0);
			}

		} catch (Exception e) {
			LogTrace.error(getClass().getSimpleName() + "/modifyPWD", e.getMessage(), e);

		}
		return null;
	}

}
